package grape.house.rest.estate.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.common.rest.mvc.BaseController;
import grape.common.tools.PinyinDto;
import grape.house.rest.estate.form.EstateCreateForm;
import grape.house.rest.estate.form.EstateListForm;
import grape.house.rest.estate.form.EstateListPageForm;
import grape.house.rest.estate.form.EstateUpdateForm;
import grape.house.rest.estate.mapper.EstateWebMapper;
import grape.house.rest.estate.vo.EstateVo;
import grape.house.service.estate.api.IEstateService;
import grape.house.service.estate.po.Estate;
import grape.house.service.estate.query.EstateListQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 楼盘 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/house/estate")
@Api(tags = "楼盘相关接口")
public class EstateController extends BaseController<EstateVo, Estate> {

    @Autowired
    private EstateWebMapper currentWebMapper;
    @Autowired
    private IEstateService currentService;



     @ApiOperation("添加楼盘")
     @PreAuthorize("hasAuthority('estate:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public EstateVo create(@RequestBody @Valid EstateCreateForm cf) {
         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }

         Estate po = currentWebMapper.formToPo(cf);
         po.setIsHasErs(false);
         po.setIsHasYs(false);
         po.setIsHasZu(false);
         setPinyin(po);
         return super.create(po);
     }

     @ApiOperation("根据ID查询楼盘")
     @PreAuthorize("hasAuthority('estate:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public EstateVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除楼盘")
     @PreAuthorize("hasAuthority('estate:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新楼盘")
     @PreAuthorize("hasAuthority('estate:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public EstateVo update(@PathVariable String id,@RequestBody @Valid EstateUpdateForm uf) {
         Estate po = currentWebMapper.formToPo(uf);
         setPinyin(po);
         po.setId(id);
         return super.update(po);
     }


    @ApiOperation("不分页查询楼盘")
    @PreAuthorize("hasAuthority('estate:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<EstateVo> list(EstateListForm listForm) {
        Estate po = currentWebMapper.formToPo(listForm);
        EstateListQuery estateListQuery = new EstateListQuery();
        estateListQuery.setPoQuery(po);
        return super.list(estateListQuery);
    }

    @ApiOperation("分页查询楼盘")
    @PreAuthorize("hasAuthority('estate:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<EstateVo> listPage(EstateListPageForm listPageForm) {
         Estate po = currentWebMapper.formToPo(listPageForm);
        EstateListQuery estateListQuery = new EstateListQuery();
        estateListQuery.setPoQuery(po);
         return super.listPage(estateListQuery,listPageForm);
     }
    /**
     * 楼盘名称汉语转拼音
     * @param estate 楼盘实体
     */
    private void setPinyin(Estate estate){
        PinyinDto pinyinDto = null;
        try {
            // 汉字转拼音
            pinyinDto = getPinyin(estate.getName(),true);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            throw new RBaseException("汉语转拼音异常", e);
        }
        estate.setSpell(Optional.ofNullable(pinyinDto.getFull()).orElse(estate.getName()));
        estate.setSpellSimple(Optional.ofNullable(pinyinDto.getSimple()).orElse(estate.getName()));
        estate.setSpellFirst(Optional.ofNullable(pinyinDto.getFirst()).orElse(estate.getName().substring(0,1)));

    }
}
